package com.zlm.user.emp.service.impl;

import com.zlm.common.base.AbstractBaseService;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.constants.CommonConstants;
import com.zlm.common.constants.FileConstants;
import com.zlm.common.constants.NumberConstants;
import com.zlm.common.entity.SysEmpAttDO;
import com.zlm.common.util.DateTimeUtils;
import com.zlm.common.util.FileUtils;
import com.zlm.user.emp.dao.SysEmpAttDao;
import com.zlm.user.emp.service.SysEmpAttService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 员工附件表
 * @Author: duyufu
 * @Date: 2019/6/25 12:00
 */
@Service
public class SysEmpAttServiceImpl extends AbstractBaseService<SysEmpAttDao, SysEmpAttDO> implements SysEmpAttService {
    @Autowired
    @Override
    public void setBaseDao(SysEmpAttDao sysEmpAttDao) {
        this.baseDao = sysEmpAttDao;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveHealthReport(Integer empId, String[] urlArray) {
        List<String> urlList = Arrays.asList(urlArray);
        try {
            int i = 0;
            List<SysEmpAttDO> empAttList = new ArrayList<>();
            for (String url : urlList) {
                i++;
                SysEmpAttDO empAtt = new SysEmpAttDO();
                empAtt.setEmpId(empId);
                empAtt.setAttType(FileConstants.IMAGES);
                empAtt.setAttUrl(url);
                empAtt.setAttName(DateTimeUtils.getCurrentDateTimeString(DateTimeUtils.FORMAT_DATETIME_NO_SEPARATOR).concat("-TJ").concat(String.valueOf(i)));
                empAtt.setAlive(NumberConstants.NUM_1);
                empAttList.add(empAtt);
            }
            baseDao.saveHealthReport(empAttList);
        } catch (Exception e) {
            e.printStackTrace();
            for (String url : urlList) {
                FileUtils.deleteFile(url);
            }
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteHealthReport(Integer empId, Integer attId) {
        SysEmpAttDO empAttDO = baseDao.queryEmpAttById(attId);
        if (empId.intValue()==empAttDO.getEmpId().intValue()) {
            baseDao.deleteHealthReport(attId);
        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
    }

    @Override
    public List<SysEmpAttDO> queryHealthReport(Integer empId) {
        //设置查看阈值
        Integer reportSize = CommonConstants.reportSize;
        List<SysEmpAttDO> sysEmpAttDOS = baseDao.queryHealthReport(empId, reportSize);
        for (SysEmpAttDO sysEmpAttDO : sysEmpAttDOS) {
            sysEmpAttDO.setAttUrl(CommonConstants.IMG_URL.concat(sysEmpAttDO.getAttUrl()));
        }
        return sysEmpAttDOS;
    }
}
